<template>
  <div>
    <el-dialog title="修改密码" :visible.sync="dialogFormVisible" @close="cleartext">
      <el-form ref="form" :model="pass" label-width="120px">
        <el-form-item label="旧密码">
          <el-input v-model="pass.old_pwd"></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="pass.new_pwd"></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input v-model="pass.renew_pwd"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="setpass">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import http from "@/util/http.js";
export default {
  props: ["type", "btn"],
  name: "",
  data() {
    return {
      dialogFormVisible: this.type,
      pass: {
        id: "",
        old_pwd: "",
        new_pwd: "",
        renew_pwd: "",
      },
    };
  },
  methods: {
    cleartext() {
      //关闭对话框后清除信息
      for (var i in this.pass) {
        this.pass[i] = "";
      }
      //调取父级函数
      this.btn();
    },
    //users/resetPassword
    setpass() {
      if (this.pass.old_pwd === "") {
        this.$message.error("请输入旧密码");
      } else if (this.pass.new_pwd === "") {
        this.$message.error("请输入新密码");
      } else if (this.pass.renew_pwd === "") {
        this.$message.error("请确认新密码");
      } else if (this.pass.new_pwd != this.pass.renew_pwd) {
        this.$message.error("新密码不一致！");
      } else {
        http.post("admin/users/resetPassword", this.pass).then(
          (res) => {
            if (res.data.code === 200) {
              console.log(res);
              this.$message({
                message: "恭喜您，用户密码修改成功",
                type: "success",
              });
              window.sessionStorage.clear();
              window.localStorage.clear();
              this.$router.push("/login");
            }
          },
          (error) => {
            this.$router.push("/login");
          }
        );
      }
    },
  },
  components: {},
  watch: {
    type(a, b) {
      //父级值改变，从而改变对话框状态
      this.dialogFormVisible = this.type;
    },
  },
};
</script>

<style scoped>
</style>
